package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeUpdateInfoParam;
import cn.tedu.csmall.product.pojo.param.SkuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.service.ISkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl implements ISkuService {

    @Autowired
    private SkuMapper mapper;

    @Override
    public void addNew(SkuAddNewParam skuAddNewParam) {
        //TODO
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始:【删除Sku】");
        log.debug("传入参数,id={}", id);

        //检查SkuID检查Sku是否存在数据，如果不存在，则抛出异常
        SkuStandardVO standardById = mapper.getStandardById(id);
        log.info("规则：根据Sku id检索数据库结果,standardById={}", standardById);
        if(standardById == null){
            String message = "删除Sku失败，Sku数据不存在！";
            log.info("结果2:根据Sku id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = mapper.deleteById(id);
        if(rows != 1){
            String message = "删除Sku失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.info("结果1:删除Sku结果,rows={}", rows);
    }

    @Override
    public SkuStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询Sku】");
        log.debug("传入参数,id={}", id);
        SkuStandardVO standardById = mapper.getStandardById(id);

        //根据SkuID检查Sku是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据Sku id查询详情，结果为null，抛出异常...");
            String message = "查询Sku详情失败，Sku数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<SkuListItemVO> listBySpuId(Long spuId, Integer pageNum) {
        Integer pageSize = 5;
        return listBySpuId(spuId, pageNum, pageSize);
    }

    @Override
    public PageData<SkuListItemVO> listBySpuId(Long spuId, Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-Sku-分页】");
        log.debug("传入参数, spuId={}, pageNum={}, pageSize={}", spuId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SkuListItemVO> list = mapper.listBySpuId(spuId);
        PageInfo<SkuListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SkuListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}
